#include <thread>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <vector>

using namespace std;

const int nums = 10;

void* routine(void* args)
{
    sleep(1);
    int cnt = 5;
    string name = (char*)args;
    while(cnt--)
    {
       cout << "线程: " <<  name << endl;
       sleep(1);
    }
    delete (char*)args;
     return 0;
}

//创建多线程 

int main()
{
    vector<pthread_t> tids;
    for(int i = 0; i < nums; i++)
    {
        pthread_t tid;
        //char id[64];
        char *id = new char[64];
        snprintf(id, 64, "thread-%d", i);
        int n = pthread_create(&tid, nullptr, routine, id);

        if(n == 0)
            tids.push_back(tid);
        //sleep(1);
    }

    for(int i = 0; i < nums; i++)
    {
        pthread_join(tids[i], nullptr);
        cout << "线程thread-" << i << "退出" << endl;
    }
    return 0;
}